package com.eam.service;

import com.yy.base.common.dao.Dao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EquipmentCycleTimelineService {
	@Autowired
	private Dao dao;
	
	/**
	 * 查找设备
	 * @return
	 */
	public Map<String, Object> searchEquipment() {
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> orgList = dao.query("SELECT AO.AD_ORG_ID,AO.NAME FROM AD_ORG AO LEFT JOIN AD_ORGTYPE AT ON AO.AD_ORGTYPE_ID = AT.AD_ORGTYPE_ID WHERE 1=1");
		String sql = "select ef.emes_facilities_id eqId,ef.name efName,ef.code efCode,ef.ad_org_id from emes_facilities ef order by ef.code";
		List<Map<String,Object>> eqList = dao.query(sql);
		map.put("orgList", orgList);
		map.put("eqList", eqList);
		return map;
	}
	/**
	 * 获取设备的履历 从设备购进、维修、保养的记录，展示单台设备履历
	 * @param equipmentId
	 * @return
	 */
	public List<Map<String, Object>> searchEquipmentCycle(String equipmentId) {
		if(StringUtils.isEmpty(equipmentId)){
			return new ArrayList<>();
		}
		String sql = "select '设备启用' planName,'start' planCode,ef.start_date startDate,null endDate,null planStartTime,null planEndTime,'START' typeName from emes_facilities ef " + 
				"where 1=1 " + 
				"and ef.start_date is not null " + 
				"and ef.emes_facilities_id=:equipmentId " + 
				"union all " + 
				"select emp.name,emp.code,emp.start_date,emp.end_date,emp.plan_state_time,emp.plan_end_time,'MAINTAIN' type from emes_maintain_plan emp " + 
				"left join emes_maintain_plan_item empi on emp.emes_maintain_plan_id=empi.emes_maintain_plan_id " + 
				"left join emes_facilities ef on ef.emes_facilities_id=empi.emes_facilities_id " + 
				"where 1=1 " + 
				"and emp.distinguish='MAINTAIN' " + 
				"and ef.emes_facilities_id=:equipmentId " + 
				"union all " + 
				"select emp.name,emp.code,emp.start_date,emp.end_date,emp.plan_state_time,emp.plan_end_time,'REPAIR' type from emes_maintain_plan emp " + 
				"left join emes_maintain_plan_item empi on emp.emes_maintain_plan_id=empi.emes_maintain_plan_id " + 
				"left join emes_facilities ef on ef.emes_facilities_id=empi.emes_facilities_id " + 
				"where 1=1 " + 
				"and emp.distinguish='REPAIR' " + 
				"and ef.emes_facilities_id=:equipmentId";
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("equipmentId", equipmentId);
		return dao.query(sql,paramMap);
	}
	
	//根据维保类型，状态进行统计
		public List<Map<String,Object>> maintainPlanDataStatistics(String orgId){
			/*
			String sqlData = "select EMP.PLANTYPE,EMP.STATUS,COUNT(1) DATACOUNT from emes_maintain_plan emp"+
						" where 1=1"+
						" and emp.distinguish='MAINTAIN'"+
						" and emp.ad_org_id=?"+
						" group by emp.plantype,emp.status";
			String sqlStatus = "select name,value from ad_ref_list where ad_reference_id='ECFC884D4AFD4615A1909AAEBE61D91B' order by seqno asc";
			String sqlMaintainType = "select name,value from ad_ref_list where ad_reference_id='273EC5F30F4840D6BCBCAE9487C95823' order by seqno asc";
			
			//维保类型
			List<Map<String,Object>> maintainTypeMap = dao.query(sqlMaintainType);
			//维保状态
			List<Map<String,Object>> maintainStatusMap = dao.query(sqlMaintainType);
			//实际数据
			List<Map<String,Object>> maintainDataMap = dao.query(sqlData, orgId);
			
			Map<String,Map<String,String>> map = new HashMap<String, Map<String,String>>();
			for(Map<String,Object> map1:maintainTypeMap){
				Map<String,String> tempMap = new HashMap<String, String>();
				for(Map<String,Object> map2:maintainStatusMap){
					tempMap.put(map2.get("VALUE")+"","0");
				}
				map.put(map1.get("VALUE")+"", tempMap);
			}
			
			for(Map<String,Object> map1:maintainDataMap){
				map.get(map1.get("PLANTYPE")+"").put(map1.get("STATUS")+"",map1.get("DATACOUNT")+"");
			}
			*/
			String sql = "select arl1.value typeValue,arl1.name typeName,arl2.value statusValue,arl2.name statusName,nvl(tempData.DATACOUNT,0) DATACOUNT from ad_ref_list  arl1"+
					" left join ad_ref_list arl2 on (arl2.ad_reference_id='ECFC884D4AFD4615A1909AAEBE61D91B' and 1=1)"+
					" left join (select EMP.PLANTYPE,EMP.STATUS,COUNT(1) DATACOUNT from emes_maintain_plan emp"+
					" where 1=1"+
					" and emp.distinguish='MAINTAIN'"+
					" and emp.ad_org_id=?"+
					" group by emp.plantype,emp.status) tempData on (arl1.value=tempData.PLANTYPE and arl2.value=STATUS)"+
					" where arl1.ad_reference_id='273EC5F30F4840D6BCBCAE9487C95823' order by arl1.seqno asc"+
					" ,decode(arl2.name,'新建',1,'待审核',2,'维保中',3,'完成',4)"
					;
			
			List<Map<String,Object>> maintainDataMap = dao.query(sql, orgId);
			return maintainDataMap;
		}
		
		//维修任务按月统计
		public Map<String,Object> equipmentRunLogStatistics(String orgId,String planDateFrom){
			if(planDateFrom==null||planDateFrom.equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
				planDateFrom = sdf.format(new Date());
			}
			String sql = "select emp.status,count(1) dataCount,to_char(nvl(emp.end_date,emp.start_date),'yyyy-MM-dd') dataDate from emes_maintain_plan emp"+
					" where 1=1"+
					" and emp.distinguish='REPAIR'"+
					" and emp.ad_org_id=?"+
					" and to_char(nvl(emp.end_date,emp.start_date),'yyyy-MM')=?"+
					" group by emp.status,to_char(nvl(emp.end_date,emp.start_date),'yyyy-MM-dd')";
			List<Map<String,Object>> maintainDataMap = dao.query(sql, orgId,planDateFrom);
			
			String machineCountSql = "select count(1) machineCount from emes_facilities where ad_org_id=? and isactive='Y'";
			Map<String,Object> machineCountMap = dao.queryUnique(machineCountSql, orgId);
			Map<String,Object> dataMap = new HashMap<String, Object>();
			dataMap.put("maintainDataMap", maintainDataMap);
			dataMap.put("machineCountMap", machineCountMap);
			return dataMap;
		}
}
